package com.dir;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.*;

/**
 * Created by wangg on 2018/7/7.
 */
public class Listener {

    public static void main(String[] args) {
        WatchService watcher = null;
        try {
            watcher = FileSystems.getDefault().newWatchService();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Path dir = Paths.get("D:\\");

        for (;;) {

            // wait for key to be signaled
            WatchKey key = null;
//            try {
//                key = watcher.take();
//            } catch (InterruptedException x) {
//                return;
//            }

            try {
                key = dir.register(watcher,
                        ENTRY_CREATE,
                        ENTRY_DELETE,
                        ENTRY_MODIFY);
            } catch (IOException x) {
                System.err.println(x);
            }

            for (WatchEvent<?> event: key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();

                // This key is registered only
                // for ENTRY_CREATE events,
                // but an OVERFLOW event can
                // occur regardless if events
                // are lost or discarded.
                if (kind == OVERFLOW) {
                    continue;
                }

                // The filename is the
                // context of the event.
                WatchEvent<Path> ev = (WatchEvent<Path>)event;
                Path filename = ev.context();


                // Verify that the new
                //  file is a text file.
                try {
                    // Resolve the filename against the directory.
                    // If the filename is "test" and the directory is "foo",
                    // the resolved name is "test/foo".
                    Path child = dir.resolve(filename);
                    System.out.println(JSONObject.toJSONString(child));
                    /*if (!Files.probeContentType(child).equals("text/plain")) {
                        System.err.format("New file '%s'" +
                                " is not a plain text file.%n", filename);
                        continue;
                    }*/
                    System.out.println(Files.isWritable(child));
                    System.out.println(Files.probeContentType(child));
                    ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
//                    Files.
//                    Files.copy(child,outputStream);
                    Files.copy(child,outputStream);
                    System.out.println(new String(outputStream.toByteArray(), Charset.forName("GB2312")));
//                    BufferedReader bread=Files.newBufferedReader(child);

//                    System.out.println(bread.ready());

//                    OutputStream out=new ByteArrayOutputStream();
//                    int b;
//                    byte[] bytes = new byte[1024 * 1024];
//                    while ((b = bread.read(bytes, 0, bytes.length)) != -1) {
//                        out.write(bytes, 0, b);
//                    }
//
//                    System.out.println();
                } catch (IOException x) {
                    System.err.println(x);
                    continue;
                }

                // Email the file to the
                //  specified email alias.
//                System.out.format("Emailing file %s%n", filename);
                //Details left to reader....
            }

            // Reset the key -- this step is critical if you want to
            // receive further watch events.  If the key is no longer valid,
            // the directory is inaccessible so exit the loop.
            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        }
    }
}
